package pl.edu.pk.mech.kkbus.repository;

import static pl.edu.pk.mech.kkbus.staticstrings.DepartureProperty.*;
import static pl.edu.pk.mech.kkbus.staticstrings.NameEntity.NAME_ENTITY_DEPARTURE;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pk.mech.kkbus.datastore.common.SynchronizeCommon;
import pl.edu.pk.mech.kkbus.model.DepartureTime;
import pl.edu.pk.mech.kkbus.model.Pojo;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DepartureDAO extends BaseDAO{

	public List<DepartureTime> getAllDepartures(){
		  List<DepartureTime> departureTimes = new ArrayList<DepartureTime>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_DEPARTURE, null, null);
		  for (Entity entity : entityList) {
			  departureTimes.add(new DepartureTime(entity));
		  }
		  return departureTimes;
	}
	
	  public DepartureTime getDeparture(Long id) {
		  Key key = KeyFactory.createKey(NAME_ENTITY_DEPARTURE, id);
		  return new DepartureTime(SynchronizeCommon.findEntity(key));
	  }

	@Override
	public void fillProperty(Pojo pojo, Entity entity) {
		DepartureTime departureTime = (DepartureTime)pojo;
		  entity.setProperty(DEP_TIME, departureTime.getTime());
		  entity.setProperty(DEP_ATTRIBUTES, departureTime.getAttributes());
		  entity.setProperty(DEP_ORDER, departureTime.getOrder());
		  entity.setProperty(DEP_ID_BUS_STOP, departureTime.getIdBusStop());
	}
}
